import 'package:card_swiper/card_swiper.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:easy_video/controller/app_controller.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:easy_video/controller/index/index.dart' as Controller;
import 'package:easy_video/model/video_model.dart';
import 'package:easy_video/widgets/auto_image_widget.dart';

class IndexView extends StatelessWidget {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      builder: (controller) {
        return Scaffold(
          key: this._scaffoldKey,
          drawer: Drawer(
            child: Container(
              color: Colors.white,
              child: ListView(
                padding: EdgeInsets.zero,
                children: [
                  DrawerHeader(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Container(
                          width: 80,
                          height: 80,
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(50),
                            child: AutoImage.buildImage(
                              AppController().user.avatar ?? "",
                            ),
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.only(
                            left: 15,
                          ),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                AppController().user.nickname.toString(),
                                style: TextStyle(
                                  fontSize: 23,
                                ),
                              ),
                              Text(
                                "ID ${AppController().user.id.toString()}",
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                  // Center(
                  //   child: ListTile(
                  //     leading: Icon(Icons.settings),
                  //     title: Text('Setting'),
                  //     onTap: () {
                  //       Navigator.pop(context);
                  //     },
                  //   ),
                  // ),
                  Align(
                    alignment: Alignment.bottomCenter,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        ListTile(
                          leading: Icon(Icons.exit_to_app),
                          title: Text('退出登录'),
                          onTap: () {
                            // 处理登出逻辑
                            AppController().exitUser();
                            Navigator.pop(context);
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          appBar: AppBar(
            automaticallyImplyLeading: false,
            title: Align(
              child: Row(
                children: [
                  Icon(
                    Icons.play_circle,
                    color: Colors.red,
                    size: 35,
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Text(
                    "Easy",
                    style: TextStyle(
                      fontWeight: FontWeight.w500,
                      fontSize: 26,
                    ),
                  ),
                ],
              ),
              alignment: Alignment.centerLeft,
            ),
            actions: [
              IconButton(
                onPressed: () {},
                icon: Icon(
                  Icons.notifications,
                  color: Colors.grey[400],
                  size: 25,
                ),
              ),
              GestureDetector(
                child: ClipRRect(
                  child: AutoImage.buildImage(
                    AppController().user.avatar ?? "",
                    width: 30,
                    height: 30,
                  ),
                  borderRadius: BorderRadius.circular(50),
                ),
                onTap: () {
                  if (!AppController().isLogin) {
                    Get.toNamed('/login');
                  } else {
                    this._scaffoldKey.currentState?.openDrawer();
                  }
                },
              ),
              SizedBox(
                width: 20,
              ),
            ],
          ),
          body: EasyRefresh(
            onLoad: () {
              controller.getVideoIndex();
            },
            onRefresh: () {
              controller.resetGet();
            },
            child: SingleChildScrollView(
              child: Container(
                // height: Get.height - 210,
                child: Column(
                  children: [
                    Container(
                      padding: EdgeInsets.only(
                        top: 10,
                        bottom: 10,
                        left: 30,
                        right: 30,
                      ),
                      child: TextField(
                        controller: controller.searchInputController,
                        decoration: InputDecoration(
                          hintText: "你想看什么?",
                          hintStyle: TextStyle(
                            color: Colors.grey,
                          ),
                          filled: true,
                          fillColor: Colors.grey[100],
                          border: OutlineInputBorder(
                            borderSide: BorderSide.none,
                            borderRadius: BorderRadius.circular(15),
                          ),
                          suffixIcon: Container(
                            padding: EdgeInsets.all(10),
                            child: IconButton(
                              color: Colors.white,
                              style: ElevatedButton.styleFrom(
                                backgroundColor: Colors.red,
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(10),
                                ),
                              ),
                              onPressed: () {
                                Get.toNamed('/search', arguments: {
                                  "keyword":
                                      controller.searchInputController.text,
                                });
                              },
                              icon: Icon(
                                Icons.search,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                    Container(
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.only(
                        top: 10,
                      ),
                      child: SingleChildScrollView(
                        scrollDirection: Axis.horizontal,
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: List.generate(
                            controller.category_list.length,
                            (index) {
                              return GestureDetector(
                                child: Container(
                                  child: Text(
                                    controller.category_list[index].Title,
                                    style: TextStyle(
                                      fontSize: 18,
                                      color:
                                          controller.current_category == index
                                              ? Colors.black
                                              : Colors.grey,
                                    ),
                                  ),
                                  padding: EdgeInsets.only(
                                    left: 30,
                                  ),
                                ),
                                onTap: () {
                                  controller.changeVideoCategory(index);
                                },
                              );
                            },
                          ),
                        ),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.all(30),
                      width: Get.width,
                      height: 250,
                      child: Swiper(
                        loop: false,
                        autoplay: false,
                        itemCount: controller.swiper_list.length,
                        itemBuilder: (ctx, index) {
                          return ClipRRect(
                            child: AutoImage.buildImage(
                              controller.swiper_list[index].Thumb,
                              fit: BoxFit.cover,
                            ),
                            borderRadius: BorderRadius.circular(20),
                          );
                        },
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(
                        left: 10,
                        right: 10,
                      ),
                      child: Wrap(
                        runSpacing: 20,
                        spacing: 20,
                        children: List.generate(
                          controller.video_list.length,
                          (index) => _item(controller.video_list[index]),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
      init: Controller.IndexController(),
    );
  }
}

class _item extends StatelessWidget {
  final VideoModel video;

  _item(this.video);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        width: Get.width / 2.5,
        height: 250,
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.5),
              spreadRadius: 5,
              blurRadius: 10,
            ),
          ],
          borderRadius: BorderRadius.circular(10),
        ),
        child: Column(
          children: [
            Flexible(
              flex: 6,
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(10),
                    topRight: Radius.circular(10),
                  ),
                  image: DecorationImage(
                    fit: BoxFit.cover,
                    image: AutoImage.buildImageProvider(
                      this.video.Thumb,
                    ),
                  ),
                ),
              ),
            ),
            Flexible(
              flex: 1,
              child: Container(
                margin: EdgeInsets.only(
                  top: 10,
                ),
                padding: EdgeInsets.only(
                  left: 10,
                ),
                alignment: Alignment.topLeft,
                child: Text(
                  this.video.Title,
                  maxLines: 1,
                  style: TextStyle(
                    overflow: TextOverflow.ellipsis,
                    fontSize: 15,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
      onTap: () {
        if (kIsWeb) {
          Get.toNamed("/web_detail/${this.video.Id}");
        } else {
          Get.toNamed("/detail/${this.video.Id}");
        }
      },
    );
  }
}
